define([ "app", "jquery", "md5", "ecb", "services/UserService",
		"resources/UserResource", "resources/AddCompanyResource" ],
		function(app) {

			var deps = [ "$scope", "$state", "$window","type","$modalInstance", "$location", "$stateParams", "UserService", "UserResource", "AddCompanyResource" ];

			function controller($scope, $state, $window,type, $modalInstance, $location, $stateParams, UserService, UserResource, AddCompanyResource) {

				$scope.type =type;
				$scope.companyCode="";
				intit($scope,AddCompanyResource);
				/**
				 * 切换鼠标经过的图片
				 */
				$scope.changeDeleteImage=function (flag){
					var carId =type;
					var image =document.getElementById("changeDeleteImage"+carId);
					if(flag){
						image.src="images/delete2.png";
					}else{
						image.src="images/delete1.png";
					}
				};
				
				/**
				 * 关闭窗口
				 */
				$scope.closeWin = function() {
					$modalInstance.close();
				};
				
				$scope.loadMore =function (){
					AddCompanyResource.queryCompanyVoById($scope.type).success(function(data){
						$scope.balance=data.balance;
						$scope.companyCode=data.companyCode;
						// 加载充值日志
						AddCompanyResource.queryRechargeLog($scope.companyCode,1,999999,$scope.type).success(function(data){
							$scope.result=data;
			            });
		            });
				};
				
				/**
				 * 充值
				 */
				$scope.recharge =function (){
					
					var rechargeAmount =$scope.rechargeAmount;
					var companyCode =$scope.companyCode;
					var id =$scope.type;
					if(rechargeAmount==null || rechargeAmount=="" || rechargeAmount ==0){
						alert("充值金额必须大于0");
						return;
					}
					AddCompanyResource.recharge(rechargeAmount,companyCode,id).success(function(data){
						
						$scope.rechargeAmount="";
						// 初始化公司数据
						AddCompanyResource.queryCompanyVoById($scope.type).success(function(data){
							$scope.balance=data.balance;
							$scope.companyCode=data.companyCode;
							// 加载充值日志
							AddCompanyResource.queryRechargeLog($scope.companyCode,1,3,$scope.type).success(function(data){
								
								$scope.result={};
								if(data.count>1){
									var array =[];
									$scope.result={};
									for(var i=0;i<data.rechargeList.length;i++){
										if(i>1){
											$scope.result.count=1;
											$scope.result.rechargeList=array;
											return;
										}
										array.push(data.rechargeList[i]);
									}
									$scope.result.rechargeList=array;
								}else{
									$scope.result.rechargeList=data.rechargeList;
									$scope.result.count=0;
								}
				            });
			            });
		            });
					
				};
				
			}
			
			/**
			 * 加载数据
			 */
			function intit($scope,AddCompanyResource){
				// 初始化公司数据
				AddCompanyResource.queryCompanyVoById($scope.type).success(function(data){
					$scope.balance=data.balance;
					$scope.companyCode=data.companyCode;
					// 加载充值日志
					AddCompanyResource.queryRechargeLog($scope.companyCode,1,3,$scope.type).success(function(data){
						$scope.result={};
						if(data.count>1){
							var array =[];
							for(var i=0;i<data.rechargeList.length;i++){
								if(i>1){
									$scope.result.count=1;
									$scope.result.rechargeList=array;
									return;
								}
								array.push(data.rechargeList[i]);
							}
							$scope.result.rechargeList=array;
						}else{
							$scope.result.rechargeList=data.rechargeList;
							$scope.result.count=0;
						}
		            });
	            });
			}
			controller.$inject = deps;
			return app.lazy.controller("CompanyRechargeController", controller);
		});
